 
import ajax from '@/api/ajax.js'
import axios from 'axios'
import url from './url.js'

import {
  getToken
} from '@/libs/util'

let createAPI = (url, params, withHeader) => {
  
  let header = {};
  if (withHeader) {
    let token = JSON.parse(getToken()).access_token;

    header = {
      headers: {
        'Authorization': 'Bearer ' + token
      }
    }
  }
  if (/\/mock\//.test(url)) { 
    return axios.get(`${url}`, params).then(res => res.data)
  }
  return ajax.post(`${url}`, params, header).then(res => res.data)
}

let api = {}
for (let key in url) {
  api[key] = (params, header, requestId) => {
    if (requestId) {
      return createAPI(url[key] + requestId, params, header);
    } else {
      return createAPI(url[key], params, header);
    }

  }
}

export default api
